package tech.aistar.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tech.aistar.entity.Com;

import java.util.List;

/**
 * Created by Administrator on 2019/10/7.
 */
public interface ComMapper {

    @Select("select * from comment")
    List<Com> findAllComment();

    @Select("select * from comment where post_id = #{postId}")
    List<Com> findAllByPostId(Integer postId);

    //根据评论id查询评论
    @Select("select * from comment where com_id = #{comId}")
    Com findComByComId(Integer comId);

    /**
     * 查询帖子一级评论
     */
    @Select("select * from comment where post_id = #{postId} and flag = 1")
    List<Com> findOneComment(Integer postId);

    //查询所有评论数
    @Select("select count(*) from comment")
    Long findAllComNum();

    //查询帖子下一级评论数
    //flag在SendCom中赋值
    @Select("select count(*) from comment where post_id = #{postId} and flag = 1")
    Long findOneComNum(Integer postId);

    //显示所有层评论下的所有二级评论
    @Select("select * from comment where floor = #{floor} and post_id = #{post_id} and flag = 0")
    List<Com> findAllTwoComment(@Param("floor") Integer floor, @Param("post_id") Integer postId);
    //二级评论数
    @Select("select count(*) from comment where floor = #{floor} and post_id = #{post_id} and flag = 0")
    Long findTwoCount(@Param("floor") Integer floor, @Param("post_id") Integer postId);

    //显示该层评论下的所有二级评论
    @Select("select * from comment where flag = #{flag} and floor = #{floor} and post_id = #{post_id}")
    List<Com> findTwoComment(@Param("floor") Integer floor, @Param("flag") Integer flag, @Param("post_id") Integer postId);

    //每层一级评论下二级评论个数
    //评论点赞数

    //根据评论者查询所有
    @Select("select * from comment where user_id = #{userId}")
    Long findAllByUserId(Integer userId);
    //根据被评论者查询所有
    @Select("select * from comment where to_user_id = #{toUserId}")
    Long findAllByToUserId(Integer toUserId);

    //根据主题id查询所有评论
    @Select("select count(*) from comment where post_id in (select post_id from post where theme_id = #{themeId})")
    Long findComNumByTheme(Integer themeId);

    /**
     * 插入评论
     */
    @Insert("insert into comment(floor,post_id,user_id,to_user_id,content,ctime,point_num,flag) values(#{floor},#{postId},#{userId},#{toUserId},#{content},#{ctime},#{pointNum},#{flag})")
    void saveComment(Com com);


}
